package bot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Created by Dominik on 2014-11-06.
 */
public class Bot extends Thread{

    private Socket socket = null;
    boolean works;
    public PrintWriter out = null;
    private BufferedReader in = null;
    private String line = "";
    private String name;
    private String shortName;

    //Bot fields

    private int round;
    private int stake;
    private int maxBet;

    private int[] botHand;
    private int botStartingCoins = -1;
    private int botCoins;
    private int botBet;
    private PlayerInfo[] arrayOfPlayers;
    private int numbersOfAddedPlayers = 0;

    public Bot(int port, String IP, String name) {
        this.name = name;
        shortName = name.trim();
        arrayOfPlayers = new PlayerInfo[5];

        try{
            this.socket  = new Socket(IP,port);
        }
        catch(IOException e){
            System.out.println("Socket problem");
            System.exit(1);
        }
    }

    public void run() {
        try{
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(),true);
            out.println("Name_"+name);
        }
        catch(IOException e){
            System.out.println("or out failed");
        }

        while(line != null) {
            try {
                line = in.readLine();

                if(line.startsWith("addNewPlayer")){
                    arrayOfPlayers[numbersOfAddedPlayers] = new PlayerInfo(line.substring(12));
                    System.out.println("** Adding player number: " + numbersOfAddedPlayers + " **");
                    numbersOfAddedPlayers++;
                }
                else if(line.startsWith("Accepted_")){
                    out.println("Name_"+name);
                }
                else if (line.startsWith("BadName_")){
                    System.out.println("Name \"" + shortName +"\" is already used");
                    System.exit(1);
                }
                else if (line.startsWith("previousPlayers_")){
                    line = line.substring(16);
                    String temp;
                    while(!line.equals("")){
                        temp = line.substring(0,15);
                        line = line.substring(15);
                        arrayOfPlayers[numbersOfAddedPlayers] = new PlayerInfo(temp);
                        System.out.println("** Adding player number: " + numbersOfAddedPlayers + " **");
                        numbersOfAddedPlayers++;
                    }
                }
                else if (line.startsWith("Chat_")){
                    //frame.displayMessage(line.substring(5));
                }
                else if (line.startsWith("SmallBlind_")){
                    String playerName = line.substring(11,26);
                    String blind = line.substring(26);
                    if(playerName.equals(name)) {
                        botBet = Integer.parseInt(blind);
                    }
                    else {
                        int i;
                        for(i=0; !(arrayOfPlayers[i].getName().equals(playerName)); i++){
                            if(i > 5) {
                                System.out.println("ERROR");
                                break;
                            }
                        }
                        arrayOfPlayers[i].setSmallBlind(true);
                        arrayOfPlayers[i].setCurrentBet(Integer.parseInt(blind));

                    }
                    //frame.setSmallBlind(line.substring(11,26),line.substring(26));
                }
                else if (line.startsWith("BigBlind_")){
                    String playerName = line.substring(9,24);
                    String blind = line.substring(24);
                    if(playerName.equals(name)) {
                        botBet = Integer.parseInt(blind);
                    }
                    else {
                        int i;
                        for(i=0; !(arrayOfPlayers[i].getName().equals(playerName)); i++){
                            if(i > 5) {
                                System.out.println("ERROR");
                                break;
                            }
                        }
                        arrayOfPlayers[i].setBigBlind(true);
                        arrayOfPlayers[i].setCurrentBet(Integer.parseInt(blind));
                    }
                    //frame.setBigBlind(line.substring(9,24),line.substring(24));
                }
                else if (line.startsWith("Cards_")){

                    for(int i = 0; i < 4; i++) {
                        botHand[i] = Integer.parseInt(line.substring(6+2*i,8+2*i));
                    }
                    //frame.displayCards(temporaryHand);
                }
                else if (line.startsWith("sCoins_")) {
                    String playerName = line.substring(7, 22);
                    String amount = line.substring(22);
                    if (playerName.equals(name)) {
                        if(botStartingCoins == -1) {
                            botStartingCoins = Integer.parseInt(amount);
                        }
                        botCoins = Integer.parseInt(amount);
                    }
                    else {
                        int i;
                        for (i = 0; !(arrayOfPlayers[i].getName().equals(playerName)); i++) {
                            if (i > 5) {
                                System.out.println("ERROR");
                                break;
                            }
                        }
                        arrayOfPlayers[i].setTotalCoins(Integer.parseInt(amount));
                    }
                }
                else if( line.startsWith("Bet_")){
                    String playerName = line.substring(4,19);
                    int bet = Integer.parseInt(line.substring(19));
                    if(bet > maxBet) {
                        maxBet = bet;
                    }
                    if (playerName.equals(name)) {
                        botBet = bet;
                    }
                    else {
                        int i;
                        for (i = 0; !(arrayOfPlayers[i].getName().equals(playerName)); i++) {
                            if (i > 5) {
                                System.out.println("ERROR");
                                break;
                            }
                        }
                        arrayOfPlayers[i].setCurrentBet(bet);
                    }
                    //frame.setBet(line.substring(4,19),line.substring(19));
                }
                else if (line.startsWith("Enable_")){
                    //frame.enableButtons(line.substring(7));
                }
                else if (line.startsWith("dFold_")){
                    String playerName = line.substring(6);
                    if (!playerName.equals(name)) {
                        int i;
                        for (i = 0; !(arrayOfPlayers[i].getName().equals(playerName)); i++) {
                            if (i > 5) {
                                System.out.println("ERROR");
                                break;
                            }
                        }
                        arrayOfPlayers[i].setFolded(true);
                    }
                    //frame.displayFold(line.substring(6));
                }
                else if (line.startsWith("dAllin_")){
                    String playerName = line.substring(7);
                    if (!playerName.equals(name)) {
                        int i;
                        for (i = 0; !(arrayOfPlayers[i].getName().equals(playerName)); i++) {
                            if (i > 5) {
                                System.out.println("ERROR");
                                break;
                            }
                        }
                        arrayOfPlayers[i].setAllIn(true);
                    }
                    //frame.displayAllin(line.substring(7));
                }
                else if (line.startsWith("Stake_")){
                    stake = Integer.parseInt(line.substring(6));
                    //frame.displayStake(line.substring(6));
                }
                else if (line.startsWith("Round_")){
                    round = Integer.parseInt(line.substring(6));
                    //frame.displayRound(line.substring(6));
                }
                else if (line.startsWith("Delete_")){
                    //frame.deletePlayer(line.substring(7));
                }
                else if (line.startsWith("Amount_")){
                    String playerName = line.substring(7,22);
                    int amountOfChangedCards = Integer.parseInt(line.substring(22));
                    if (!playerName.equals(name)) {
                        int i;
                        for (i = 0; !(arrayOfPlayers[i].getName().equals(playerName)); i++) {
                            if (i > 5) {
                                System.out.println("ERROR");
                                break;
                            }
                        }
                        arrayOfPlayers[i].setRecentlyChangedCards(amountOfChangedCards);
                    }
                    //frame.displayAmountOfExchangedCards(line.substring(7,22),line.substring(22));
                }

            }
            catch(IOException e){
                System.out.println("Read failed");
                System.exit(1);
            }
        }
    }

    public void sendBet(int bet){
        out.println("Bet_" + Integer.toString(bet));
    }
    public void sendCheck(){
        out.println("Check_");
    }
    public void sendAllin(){
        out.println("All_");
    }
    public void sendFold(){
        out.println("Fold_");
    }
    public void sendExchange(String cardSet){
        out.println("Exchange_" + cardSet);
    }
    public void sendClose(){
        out.println("Close_");
    }




}
